<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<title>扫一扫</title>
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
	<link href="../../css/mui.min.css" rel="stylesheet"/>
	<style type="text/css">
		.code {
			position: absolute;
			top: 45px; left: 0;
			width: 100%; height: 100%;
			background: #F0F0F0;
		}
	</style>
</head>
<body>
	<!-- 头部TITLE -->
	<header class="mui-bar mui-bar-nav">
		<span class="mui-icon mui-icon-back" 
			onclick="jumpToMy()"></span>
		<h1 class="mui-title">扫一扫</h1>
	</header>
	<!-- 头部TITLE -->
	
	<!-- 主体部分 -->
	<div id="code" class="code" style="">
		<!--盛放扫描控件的div-->
	</div>
	<!-- 主体部分 -->
	
	<!-- 底部菜单 -->
	<nav class="mui-bar mui-bar-tab">
		<a class="mui-tab-item" onclick="jumpToIndex()">
			<span class="mui-icon mui-icon-home"></span>
			<span class="mui-tab-label">首页</span>
		</a>
		<a class="mui-tab-item" onclick="jumpToMarket()">
			<span class="mui-icon mui-icon-download"></span>
			<span class="mui-tab-label">库</span>
		</a>
<!-- 		<a class="mui-tab-item" onclick="jumpToMarket()">
			<span class="mui-icon mui-icon-download"></span>
			<span class="mui-tab-label">广场</span>
		</a> -->
<!-- 		<a class="mui-tab-item" onclick="jumpToCamera()">
			<span class="mui-icon mui-icon-camera"></span>
			<span class="mui-tab-label">拍</span>
		</a> -->
<!-- 		<a class="mui-tab-item" onclick="jumpToChat()">
			<span class="mui-icon mui-icon-chatbubble"></span>
			<span class="mui-tab-label">消息</span>
		</a> -->
		<a class="mui-tab-item mui-active" onclick="jumpToMy()">
			<span class="mui-icon mui-icon-contact"></span>
			<span class="mui-tab-label">我的</span>
		</a>
	</nav>
	<!-- 底部菜单 -->
</body>
</html>

<script src="../../js/mui.js"></script>
<script src="../../js/app.js"></script>
<script type="text/javascript">
	scan = null; //扫描对象  
	mui.plusReady(function() {
		mui.init();
		startRecognize();
	});
	// 开始扫描
	function startRecognize() {
		try {
			var filters;
			//自定义的扫描控件样式  
			var styles = {
				frameColor: "#0088CC",
				scanbarColor: "#0088CC"
			};
			//扫描控件构造  
			scan = new plus.barcode.Barcode(
				'code', filters, styles
			);
			scan.onmarked = onmarked;
			scan.onerror = onerror;
			scan.start();
		} catch(e) {
			mui.alert("出现错误啦:\n" + e);
		}
	};
	// 扫码错误
	function onerror(e) {
		miu.alert(e);
	};
	// 扫码识别
	function onmarked(type, result, file){
		switch(type){
			case plus.barcode.QR:
			type = 'QR';
			break;
			case plus.barcode.EAN13:
			type = 'EAN13';
			break;
			case plus.barcode.EAN8:
			type = 'EAN8';
			break;
			default:
			type = '其它'+type;
			break;
		}
		result = result.replace(/\n/g, '');
		result = result.replace(/\"/g, ''); 
		//分析扫描结果：是URL就跳转 ，不是就提示
		if(result.indexOf('http://')==0  || 
			result.indexOf('https://')==0){
			// 是链接则跳转
			// plus.runtime.openURL(result); //第三方浏览器
			// plus.webview.open(result); //APP内部
			jumpToMyScanWeb(result);
			scan.cancel();
		}else{
			// 不是链接则显示结果
			mui.alert(result);
		}
	}
	// 从相册中选择二维码图片   
	function scanPicture() {
		plus.gallery.pick(function(path) {
			plus.barcode.scan(path, onmarked, function(error) {
				plus.nativeUI.alert("无法识别此图片");
			});
			
		});
	}
	//返回关闭条码识别控件不然识别控件会一直显示
	function goReturn() {
		scan.cancel();//结束条码识别
		scan.close(); //关闭条码识别控件
		mui.back();
	}                        
	
	//子页面按键返回
	document.addEventListener('plusready', function() {
		var webview = plus.webview.currentWebview();
		plus.key.addEventListener('backbutton', function() {
			scan.close(); //关闭扫码条件
			webview.canBack(function(e) {
				if(e.canBack) {
					webview.back();
				} else {
					webview.close();
				}
			})
		});
	});
</script>